package com.scau402.system1.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scau402.system1.domain.dto.CalWarehouseNumAndValueDTO;
import com.scau402.system1.domain.dto.WarehouseStorage;
import com.scau402.system1.domain.po.WmsCheckboundJadeDetailPO;
import com.scau402.system1.domain.po.WmsJadePO;
import com.scau402.system1.domain.vo.WmsJadeImageAndImageIdVO;
import com.scau402.system1.domain.vo.WmsJadeVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 单个翡翠信息Mapper接口V2
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@Mapper
public interface WmsJadeMapperV2 extends BaseMapper<WmsJadePO> {
    /**
     * 根据仓位id搜索翡翠详情
     * @param ids 仓位id们
     */
    @Deprecated
    @Select("select jade_name,jade_barcode,warehouse_id,sum(jade_quantity) as jade_quantity,sum(outbound_quantity) as outboound_quantity,jade_cost,jade_tag_price,jade_market_price " +
            "from wms_jade " +
            "where storage_id in #{ids} " +
            "group by jade_barcode,jade_name,jade_barcode,warehouse_id,jade_cost,jade_tag_price,jade_market_price")
    List<WmsJadePO> selectJadeDetailByStorageId(List<Long> ids);
    /**
     * 根据仓库id搜索翡翠详情
     * @param warehouseId 仓库id
     */
//    @Select("select jade_name,jade_barcode,storage_id,warehouse_id," +
//            "sum(jade_quantity) as jade_quantity,sum(outbound_quantity) as outbound_quantity," +
//            "jade_cost,jade_tag_price,jade_market_price,image_id " +
//            "from wms_jade " +
//            "where warehouse_id = #{warehouseId} " +
//            "group by jade_barcode,jade_name,storage_id,warehouse_id,jade_cost,jade_tag_price,jade_market_price")
    List<WmsCheckboundJadeDetailPO> selectJadeDetailByWarehouseId(Long warehouseId);

    /**
     * 根据翡翠条码查询一系列翡翠信息
     * @param barcode 翡翠条码
     * @return
     */
//    @Select("select jade_barcode,jade_name,storage_id,warehouse_id,sum(jade_quantity) as jade_quantity,sum(outbound_quantity) as outbound_quantity,jade_cost,jade_tag_price,jade_market_price " +
//            "from wms_jade wj" +
//            "LEFT JOIN wms_warehouse ww ON wj.warehouse_id = ww.id" +
//            " jade_barcode = #{barcode} " +
//            "group by jade_barcode,jade_name,storage_id,warehouse_id,jade_cost,jade_tag_price,jade_market_price;")
    WmsJadeVO selectAdjustJadeDetailByBarCode(String barcode);
    /**
     * 通过jadeId获取所在仓库
     * @param id 翡翠id
     * @return
     */
    @Select("SELECT warehouse_id from wms_jade where id = #{id}")
    Long selectWarehouseIdById(@Param("id") Long id);

    /**
     * 通过jadeId获取所在仓位
     * @param id 翡翠id
     * @return
     */
    @Select("")
    Long selectStorageIdById(Long id);
    /**
     * 计算仓库的在仓总价值
     * @param warehouseId 仓库id
     * @return
     */
    //在仓总价值 = sum(一种翡翠单成本*(总量-出库量))
    @Select("select count(id) as totalNum,sum(jade_cost*(jade_quantity-outbound_quantity)) as totalValue from wms_jade where warehouse_id = #{warehouseId};")
    CalWarehouseNumAndValueDTO selectTotalPriceByWarehouseId(@Param("warehouseId") Long warehouseId);

    /**
     * 计算仓位的在仓总价值
     * @param storageId 仓位id
     * @return
     */
    @Select("select count(id) as totalNum,sum(jade_cost*(jade_quantity-outbound_quantity)) as totalValue from wms_jade where storage_id = #{storageId};")
    CalWarehouseNumAndValueDTO selectTotalPriceByStorageId(@Param("storageId") Long storageId);


    /**
     * 获取翡翠条码对应的仓库id和仓位id
     * @param barcode 翡翠条码
     * @return
     */
    @Select("select warehouse_id,storage_id from wms_jade where jade_barcode = #{barcode} group by jade_barcode,warehouse_id,storage_id;")
    WmsJadePO selectWareAStoIdByBarcode(@Param("barcode") String barcode);

    /**
     * 通过翡翠条码获取一个imageId
     * @param jadeBarcode 翡翠条码
     * @return
     */
    // TODO: 查询翡翠图片
    @Select("select id AS imageId,image_url from wms_jade_image WHERE id IN (select wms_jade.image_id from wms_jade where jade_barcode = #{jadeBarcode}) limit 0,1")
    WmsJadeImageAndImageIdVO selectImageIdByBarcode(@Param("jadeBarcode") String jadeBarcode);

    @Update("update wms_jade set del_flag = #{jadeVisible} where id = #{jadeId}")
    void updateVisibleByJadeId(@Param("jadeId") Long jadeId,@Param("jadeVisible") String jadeVisible);

    @Update("update wms_jade set status = #{jadeStatus} where id = #{jadeId}")
    void updateStatusByJadeId(@Param("jadeId")Long jadeId,@Param("jadeStatus") String jadeStatus);

    @Select("select storage_id,(jade_quantity - outbound_quantity) as jade_quantity from wms_jade where id = #{jadeId}")
    WmsJadePO findQuantityByJadeId(@Param("jadeId") Long jadeId);

    CalWarehouseNumAndValueDTO selectTotalPriceByWareStorageId(List<WarehouseStorage> targetList);

    List<WmsCheckboundJadeDetailPO> selectJadeDetailByRelatedStorageIds(@Param("warehouseId") Long warehouseId,@Param("list")  List<Long> relatedStorageIds);

    List<WmsCheckboundJadeDetailPO> selectJadeDetailListByWarehouseStorage(List<WarehouseStorage> targetList);
}